package com.jinsilu.freight.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ObjectUtils;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.R;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityMyVehicleAddBinding;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.helper.ShowImageHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.model.HttpListData;
import com.jinsilu.freight.http.request.GetLicensePlateColorApi;
import com.jinsilu.freight.http.request.GetVehicleLengthApi;
import com.jinsilu.freight.http.request.OCRApi;
import com.jinsilu.freight.http.request.UpdateImageApi;
import com.jinsilu.freight.http.request.VerifySingleVehicleApi;
import com.jinsilu.freight.http.response.DictBean;
import com.jinsilu.freight.http.response.FileBean;
import com.jinsilu.freight.http.response.GetSingleVehicleBean;
import com.jinsilu.freight.http.response.LicensePlateColor;
import com.jinsilu.freight.http.response.OCRDrivingBean;
import com.jinsilu.freight.http.response.VehicleLength;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.dialog.DateDialog;
import com.jinsilu.freight.ui.dialog.MenuDialog;
import com.king.keyboard.KingKeyboard;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 添加车辆页面
 *
 * @author yangyang
 * @time 2020-11-11 16:43
 */
public class MyVehicleAddActivity extends MyVBActivity<ActivityMyVehicleAddBinding> {

    private int mCurrentSelectIndex; // 当前选择上传的图片条目索引

    private String vehicleId; // 车辆信息 编辑用

    private DictBean dictBean;

    private String vehicleFront, vehicleCopy, roadTransport, insurance; // 图片地址
    private String vehicleFrontPhoto; // 车头照片地址

    private KingKeyboard vehicleKeyBoard;

    private List<LicensePlateColor> licensePlateColors; // 车牌颜色列表
    private String licensePlateColor = ""; // 车牌颜色
    private String licensePlateColorCode = ""; // 车牌颜色编码

    private BaseDialog licensePlateColorDialog;
    private BaseDialog vehicleLengthDialog; // 车辆长度选择对话框

    private List<VehicleLength> vehicleLengths; // 车辆长度列表
    private String selectedVehicleLength = ""; // 选中的车辆长度
    private String selectedVehicleLengthCode = ""; // 选中的车辆长度编码

    private GetSingleVehicleBean mSingleVehicleBean;

    @Override
    protected void initView() {

        if (getIntent().hasExtra("vehicle")) {
            vehicleId = (String) getIntent().getSerializableExtra("vehicle");
            getSingleVehicleLicenseStatus();
        } else {
            mSingleVehicleBean = new GetSingleVehicleBean();
            GetSingleVehicleBean.VehicleLicenseCertificationResultBean vehicleLicenseCertificationResultBean = new GetSingleVehicleBean.VehicleLicenseCertificationResultBean();
            mSingleVehicleBean.setVehicleLicenseCertificationResult(vehicleLicenseCertificationResultBean);
        }

        setOnClickListener(
                binding.ivVehicleLicense, binding.ivVehicleLicenseCopy,
                binding.ivRoadTransportPhoto,
                binding.ivPolicyOfInsurancePhoto, binding.tvPolicyOfInsuranceName,
                binding.tvUploadVehicleLicenseStatus,
                binding.tvUploadRoadTransportStatus, binding.tvUploadPolicyOfInsuranceStatus,
                binding.tvBtnSubmitCertificate, binding.tvLicensePlateNumber,
                binding.tvExpirationDate, binding.ivVehicleHeadImg, binding.tvVehicleHeadImg,
                binding.tvVehicleLength);

        vehicleKeyBoard = new KingKeyboard(this, binding.keyboardParent);
        vehicleKeyBoard.register(binding.etVehicleLicenseNumber, KingKeyboard.KeyboardType.LICENSE_PLATE);
    }

    @Override
    protected void initData() {
        getLicensePlateColor();
        getVehicleLength();
    }

    /**
     * 获取车牌颜色
     */
    private void getLicensePlateColor() {
        EasyHttp.get(this)
                .api(new GetLicensePlateColorApi())
                .request(new HttpCallback<HttpListData<LicensePlateColor>>(this) {

                    @Override
                    public void onSucceed(HttpListData<LicensePlateColor> data) {
                        licensePlateColors = data.getData();
                        List<String> plateColorList = new ArrayList<>();
                        for (LicensePlateColor plateColor :
                                licensePlateColors) {
//                            if ("Y".equals(plateColor.getIsDefault())) // 设置默认车牌颜色
//                                binding.tvLicensePlateNumber.setText(plateColor.getDictLabel());
                            plateColorList.add(plateColor.getDictLabel());
                        }
                        initBottomSelectDialog(plateColorList);
                    }
                });
    }

    private void initBottomSelectDialog(List<String> data) {
        // 底部选择框
        licensePlateColorDialog = new MenuDialog.Builder(this)
                // 设置 null 表示不显示取消按钮
                //.setCancel(getString(R.string.common_cancel))
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(data)
                .setListener(new MenuDialog.OnListener<String>() {

                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        licensePlateColor = licensePlateColors.get(position).getDictLabel();
                        licensePlateColorCode = licensePlateColors.get(position).getDictValue();
                        mSingleVehicleBean.getVehicleLicenseCertificationResult().setLicensePlateColor(licensePlateColor);
                        mSingleVehicleBean.getVehicleLicenseCertificationResult().setLicensePlateColorCode(licensePlateColorCode);
                        binding.tvLicensePlateNumber.setText(string);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        if (dialog.isShowing()) dialog.dismiss();
                    }
                })
                .create();
    }

    /**
     * 获取车辆长度
     */
    private void getVehicleLength() {
        EasyHttp.get(this)
                .api(new GetVehicleLengthApi())
                .request(new HttpCallback<HttpListData<VehicleLength>>(this) {
                    @Override
                    public void onSucceed(HttpListData<VehicleLength> data) {
                        try {
                            vehicleLengths = data.getData();
                            List<String> lengthList = new ArrayList<>();
                            for (VehicleLength length : vehicleLengths) {
                                lengthList.add(length.getDictLabel());
                            }
                            initVehicleLengthDialog(lengthList);
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void initVehicleLengthDialog(List<String> data) {
        vehicleLengthDialog = new MenuDialog.Builder(this)
                .setList(data)
                .setListener(new MenuDialog.OnListener<String>() {
                    @Override
                    public void onSelected(BaseDialog dialog, int position, String string) {
                        selectedVehicleLength = vehicleLengths.get(position).getDictLabel();
                        selectedVehicleLengthCode = vehicleLengths.get(position).getDictValue();
                        binding.tvVehicleLength.setText(string);
                        if (mSingleVehicleBean != null && mSingleVehicleBean.getVehicleLicenseCertificationResult() != null) {
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setVehicleLength(string);
                        }
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        if (dialog.isShowing()) dialog.dismiss();
                    }
                })
                .create();
    }

    /**
     * 获单个车辆认证状态
     */
    private void getSingleVehicleLicenseStatus() {
        EasyHttp.get(this)
//                .api(new GetSingleVehicleStatusApi().setVehicleId(vehicleId))
                .api(String.format("api/certification/vehicle/%s", vehicleId))
                .request(new HttpCallback<HttpData<GetSingleVehicleBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<GetSingleVehicleBean> data) {
                        mSingleVehicleBean = data.getData();
                        setContent(mSingleVehicleBean);
                    }
                });
    }

    private void setContent(GetSingleVehicleBean singleVehicleBean) {
        /*编辑 数据回填*/
        licensePlateColor = singleVehicleBean.getVehicleLicenseCertificationResult().getLicensePlateColor();
        licensePlateColorCode = singleVehicleBean.getVehicleLicenseCertificationResult().getLicensePlateColorCode();
        binding.tvLicensePlateNumber.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getLicensePlateColor());

        vehicleFront = singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseFrontUrl();
        vehicleCopy = singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseReverseUrl();
        if (!TextUtils.isEmpty(singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseFrontUrl())) {
            ShowImageHelper.showNormal(this, singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseFrontUrl(), binding.ivVehicleLicense);
        }
        if (!TextUtils.isEmpty(singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseReverseUrl())) {
            ShowImageHelper.showNormal(this, singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseReverseUrl(), binding.ivVehicleLicenseCopy);
        }
        if (!TextUtils.isEmpty(singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleFrontPhoto())) {
            vehicleFrontPhoto = singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleFrontPhoto();
            ShowImageHelper.showNormal(this, singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleFrontPhoto(), binding.ivVehicleHeadImg);
        }
        binding.etVehicleLicenseNumber.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getPlateNumber());
        binding.etVehicleIdentificationCode.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseIdCode());
        binding.etVehicleIdentificationNumber.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleFrameNumber());
        binding.etApprovedLoad.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getApprovedLoad());
        binding.etTotalMass.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getGrossMass());
        binding.tvExpirationDate.setText(singleVehicleBean.getVehicleLicenseCertificationResult().getValidityEndDate());

        if (ObjectUtils.isEmpty(singleVehicleBean.getVehicleTransportLicenseCertificationResult()))
            return;
        roadTransport = singleVehicleBean.getVehicleTransportLicenseCertificationResult().getVehicleTransportLicense();
        ShowImageHelper.showNormal(this, singleVehicleBean.getVehicleTransportLicenseCertificationResult().getVehicleTransportLicense(), binding.ivRoadTransportPhoto);
        binding.etRoadTransportNumber.setText(singleVehicleBean.getVehicleTransportLicenseCertificationResult().getVehicleTransportLicenseNumber());

//        insurance = singleVehicleBean.getInsuranceCertificationResult().getInsuranceCertificate();
//        ShowImageHelper.showNormal(this, singleVehicleBean.getInsuranceCertificationResult().getInsuranceCertificate(), binding.ivPolicyOfInsurancePhoto);
//        binding.etPolicyNumber.setText(singleVehicleBean.getInsuranceCertificationResult().getInsuranceCertificateNumber());
//        binding.tvPolicyOfInsuranceName.setText(singleVehicleBean.getInsuranceCertificationResult().getInsuranceCompany());

        // 设置车辆长度
        if (!TextUtils.isEmpty(singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleLength())) {
            selectedVehicleLength = singleVehicleBean.getVehicleLicenseCertificationResult().getVehicleLength();
            binding.tvVehicleLength.setText(selectedVehicleLength);
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.tvExpirationDate) {
            showDateDialog();
        } else if (v == binding.ivVehicleLicense) {
            mCurrentSelectIndex = 0;
            showTipsDialog();
        } else if (v == binding.ivVehicleLicenseCopy) {
            mCurrentSelectIndex = 1;
            showTipsDialog();
        } else if (v == binding.ivRoadTransportPhoto) {
            mCurrentSelectIndex = 2;
            startImageSelectActivity(0);
        } else if (v == binding.ivPolicyOfInsurancePhoto) {
            mCurrentSelectIndex = 3;
            startImageSelectActivity(0);
        } else if (v == binding.ivVehicleHeadImg || v == binding.tvVehicleHeadImg) {
            mCurrentSelectIndex = 4;
            startImageSelectActivity(0);
        } else if (v == binding.tvPolicyOfInsuranceName) {
            startActivityForResult(InsuranceCompanyListActivity.class, new OnActivityCallback() {
                @Override
                public void onActivityResult(int resultCode, @Nullable Intent data) {
                    if (RESULT_OK == resultCode && null != data) {
                        dictBean = (DictBean) data.getSerializableExtra("insurance");
                        binding.tvPolicyOfInsuranceName.setText(null == dictBean ? "" : dictBean.getDictLabel());
                    }
                }
            });
        } else if (binding.tvLicensePlateNumber.equals(v)) {
            if (null != licensePlateColorDialog) licensePlateColorDialog.show();
            else getLicensePlateColor();
        } else if (v == binding.tvVehicleLength) {
            showVehicleLengthDialog();
        } else if (v == binding.tvBtnSubmitCertificate) {
            submitInfo();
        }
    }

    private BaseDialog expirationDialog;

    private void showDateDialog() {
        // 日期选择对话框
        if (null == expirationDialog) {
            expirationDialog = new DateDialog.Builder(this)
                    .setTitle(getString(R.string.date_title))
                    // 确定按钮文本
                    .setConfirm(getString(R.string.common_confirm))
                    // 设置 null 表示不显示取消按钮
                    .setCancel(getString(R.string.common_cancel))
                    // 设置日期
                    //.setDate("2018-12-31")
                    //.setDate("20181231")
                    //.setDate(1546263036137)
                    // 设置年份
                    //.setYear(2018)
                    // 设置月份
                    //.setMonth(2)
                    // 设置天数
                    //.setDay(20)
                    // 不选择天数
                    //.setIgnoreDay()
                    .setListener(new DateDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, int year, int month, int day) {
//                            toast(year + getString(R.string.common_year) + month + getString(R.string.common_month) + day + getString(R.string.common_day));
                            // 如果不指定时分秒则默认为现在的时间
//                            Calendar calendar = Calendar.getInstance();
//                            calendar.set(Calendar.YEAR, year);
                            // 月份从零开始，所以需要减 1
//                            calendar.set(Calendar.MONTH, month - 1);
//                            calendar.set(Calendar.DAY_OF_MONTH, day);
//                            toast("时间戳：" + calendar.getTimeInMillis());
                            //toast(new SimpleDateFormat("yyyy年MM月dd日 kk:mm:ss").format(calendar.getTime()));
                            binding.tvExpirationDate.setText(String.format("%s-%s-%s", year, month, day));
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {

                        }
                    })
                    .show();
        } else {
            expirationDialog.show();
        }
    }

    /**
     * 单个车辆认证
     */
    private void submitInfo() {
        if (TextUtils.isEmpty(binding.tvLicensePlateNumber.getText().toString().trim())) {
            toast("请选择车牌号颜色");
            return;
        }
        if (TextUtils.isEmpty(vehicleFront)) {
            toast("请上传驶证正本");
            return;
        }
        if (TextUtils.isEmpty(vehicleCopy)) {
            toast("请上传驶证副本");
            return;
        }

        String plateNumber = binding.etVehicleLicenseNumber.getText().toString().trim();
        if (TextUtils.isEmpty(plateNumber)) {
            toast("请输入车牌号");
            return;
        }

        String vehicleFrameNumber = binding.etVehicleIdentificationNumber.getText().toString().trim();


        String validate = binding.tvExpirationDate.getText().toString().trim();
        String approved = binding.etApprovedLoad.getText().toString().trim();
        if (!TextUtils.isEmpty(approved)) {
            if (Integer.parseInt(approved) < 1000){
                toast("请输入合法的核定载重");
                return;
            }
        }
        String grossMass = binding.etTotalMass.getText().toString().trim();
        if (!TextUtils.isEmpty(grossMass)) {
            if (Integer.parseInt(grossMass) < 1000){
                toast("请输入合法的总质量");
                return;
            }
        }
        if (TextUtils.isEmpty(validate)) {
            toast("请选择有效期止");
            return;
        }

        if (TextUtils.isEmpty(roadTransport)) {
            toast("请上传道路运输证");
            return;
        }
        String qualificationNumber = binding.etRoadTransportNumber.getText().toString().trim();
        if (TextUtils.isEmpty(qualificationNumber)) {
            toast("请输入道路运输证号");
            return;
        }


//        if (TextUtils.isEmpty(insurance)) {
//            toast("请上传保险单照片");
//            return;
//        }
//        String insuranceNumber = binding.etPolicyNumber.getText().toString().trim();
//        if (TextUtils.isEmpty(insuranceNumber)) {
//            toast("请输入保险单号");
//            return;
//        }
//        String insuranceName = binding.tvPolicyOfInsuranceName.getText().toString().trim();
//        if (TextUtils.isEmpty(insuranceName)) {
//            toast("请选择保险公司名称");
//            return;
//        }

        EasyHttp.post(this)
                .api(new VerifySingleVehicleApi()
                                .setVehicleId(vehicleId)
                                .setLicensePlateColor(licensePlateColor)
                                .setLicensePlateColorCode(licensePlateColorCode)
                                .setDrivingLicenseFrontUrl(vehicleFront)
                                .setDrivingLicenseReverseUrl(vehicleCopy)
                                .setDrivingLicenseIdCode(mSingleVehicleBean.getVehicleLicenseCertificationResult().getDrivingLicenseIdCode())
                                .setPlatNumber(plateNumber)
                                .setDrivingLicenseIssuingAuthority(mSingleVehicleBean.getVehicleLicenseCertificationResult().getIssuingAuthority())
                                .setVehicleTransportLicense(roadTransport)
                                .setVehicleTransportLicenseNumber(qualificationNumber)
                                .setVehicleFrontPhoto(vehicleFrontPhoto)
                                .setVehicleLength(selectedVehicleLength)
                                .setVehicleLengthCode(selectedVehicleLengthCode)
                                .setVehicleFrameNumber(vehicleFrameNumber)
                                .setEnergyType(mSingleVehicleBean.getVehicleLicenseCertificationResult().getEnergyType())
                                .setEngineNumber(mSingleVehicleBean.getVehicleLicenseCertificationResult().getEngineNumber())
                                .setGrossMass(grossMass)
                                .setIssueDate(mSingleVehicleBean.getVehicleLicenseCertificationResult().getIssueDate())
                                .setApprovedLoad(approved)
                                .setRegisterDate(mSingleVehicleBean.getVehicleLicenseCertificationResult().getRegisterDate())
                                .setUseCharacter(mSingleVehicleBean.getVehicleLicenseCertificationResult().getUseCharacter())
                                .setValidityEndDate(validate)
                                .setVehicleModel(mSingleVehicleBean.getVehicleLicenseCertificationResult().getVehicleModel())
                                .setVehicleOwner(mSingleVehicleBean.getVehicleLicenseCertificationResult().getVehicleOwner())
                                .setVehicleType(mSingleVehicleBean.getVehicleLicenseCertificationResult().getVehicleType())
                )
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        toast(data.getMessage());
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onFail(Exception e) {
                        CommonHelper.toastErrorMsg(e, getString(R.string.plz_reupload_vehicle_image));
                    }
                });
    }

    /**
     * 启动图片选择
     */
    private void startImageSelectActivity(int index) {
        ImageSelectVBActivity.start(this, new ImageSelectVBActivity.OnPhotoSelectListener() {

            @Override
            public void onSelected(File file) {
//                File file = CommonHelper.getFileFromUri(data);
                uploadImage(file);
            }

            @Override
            public void onCancel() {
            }
        });
    }

    private void uploadImage(File file) {
        // 上传文件
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setImage(file))
                .request(new HttpCallback<HttpData<FileBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<FileBean> data) {
//                        FileUtils.delete(file);
                        String imgUrl = data.getData().getUrl();
                        if (mCurrentSelectIndex == 0) {
                            ocr(MTLConstants.DRIVING_FRONT, imgUrl);
                        } else if (mCurrentSelectIndex == 1) {
                            ocr(MTLConstants.DRIVING_BACK, imgUrl);
                        } else if (mCurrentSelectIndex == 2) {
                            roadTransport = imgUrl;
                            ShowImageHelper.showNormal(MyVehicleAddActivity.this, imgUrl, binding.ivRoadTransportPhoto);
                        } else if (mCurrentSelectIndex == 3) {
                            insurance = imgUrl;
                            ShowImageHelper.showNormal(MyVehicleAddActivity.this, imgUrl, binding.ivPolicyOfInsurancePhoto);
                        } else if (mCurrentSelectIndex == 4) {
                            vehicleFrontPhoto = imgUrl;
                            ShowImageHelper.showNormal(MyVehicleAddActivity.this, imgUrl, binding.ivVehicleHeadImg);
                        }
                    }
                });
    }

    private BaseDialog tipsDialog;

    private void showTipsDialog() {
        // 自定义对话框
        if (null == tipsDialog) {
            tipsDialog = new BaseDialog.Builder(this)
                    .setContentView(R.layout.dialog_certificate_tips)
                    .setAnimStyle(BaseDialog.ANIM_SCALE)
                    //.setText(id, "我是预设置的文本")
                    .setGravity(Gravity.BOTTOM)
                    .setOnClickListener(R.id.tv_know, (BaseDialog.OnClickListener<TextView>) (dialog, view) ->
                            {
//                                showMenuDialog();
                                startImageSelectActivity(-1);
                                dialog.dismiss();
                            }

                    )
                    .show();
        } else {
            tipsDialog.show();
        }
    }

    /**
     * orc 识别
     *
     * @param type   类型【1身份证人像面；2身份证国徽面；3驾驶证正页；4驾驶证副页；5行驶证正页；6行驶证副页；7车辆道路运输证】
     * @param imgUrl 图片地址
     */
    private void ocr(String type, String imgUrl) {
        EasyHttp.post(this)
                .api(new OCRApi()
                        .setType(type)
                        .setUrl(imgUrl)
                )
                .request(new HttpCallback<HttpData<OCRDrivingBean>>(this) {

                    @Override
                    public void onSucceed(HttpData<OCRDrivingBean> data) {
                        OCRDrivingBean ocrDrivingBean = data.getData();
                        if (ObjectUtils.isEmpty(ocrDrivingBean)) {
                            toast(R.string.plz_reupload_vehicle_image);
                            return;
                        }
                        if (mCurrentSelectIndex == 0) {
                            if (ObjectUtils.isEmpty(mSingleVehicleBean)) {
                                mSingleVehicleBean = new GetSingleVehicleBean();
                                GetSingleVehicleBean.VehicleLicenseCertificationResultBean vehicleLicenseCertificationResultBean = new GetSingleVehicleBean.VehicleLicenseCertificationResultBean();
                                mSingleVehicleBean.setVehicleLicenseCertificationResult(vehicleLicenseCertificationResultBean);
                            }
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setDrivingLicenseFrontUrl(imgUrl);
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setDrivingLicenseIdCode(ocrDrivingBean.getVin());

                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setEngineNumber(ocrDrivingBean.getEngineNo());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setPlateNumber(ocrDrivingBean.getNumber());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setRegisterDate(ocrDrivingBean.getRegisterDate());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setUseCharacter(ocrDrivingBean.getUseCharacter());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setVehicleModel(ocrDrivingBean.getModel());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setVehicleOwner(ocrDrivingBean.getName());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setIssuingAuthority(ocrDrivingBean.getIssuingAuthority());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setVehicleType(ocrDrivingBean.getVehicleType());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setIssueDate(ocrDrivingBean.getIssueDate());
                        } else {
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setDrivingLicenseReverseUrl(imgUrl);
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setGrossMass(ocrDrivingBean.getGrossMass());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setApprovedLoad(ocrDrivingBean.getApprovedLoad());
                            mSingleVehicleBean.getVehicleLicenseCertificationResult().setEnergyType("柴油");
                            if (!TextUtils.isEmpty(ocrDrivingBean.getInspectionRecord())) {
                                if (ocrDrivingBean.getInspectionRecord().contains("柴油")) {
                                    mSingleVehicleBean.getVehicleLicenseCertificationResult().setEnergyType("柴油");
                                } else if (ocrDrivingBean.getInspectionRecord().contains("天然气")) {
                                    mSingleVehicleBean.getVehicleLicenseCertificationResult().setEnergyType("天然气");
                                }
                            }
                        }
                        setContent(mSingleVehicleBean);
                    }

                    @Override
                    public void onFail(Exception e) {
//                        super.onFail(e);
                        toast(R.string.plz_reupload_vehicle_image);
                    }
                });
    }

    private void showVehicleLengthDialog() {
        if (null == vehicleLengthDialog) {
            vehicleLengthDialog = new MenuDialog.Builder(this)
                    .setList(vehicleLengths)
                    .setListener(new MenuDialog.OnListener<String>() {
                        @Override
                        public void onSelected(BaseDialog dialog, int position, String string) {
                            selectedVehicleLength = string;
                            binding.tvVehicleLength.setText(string);
                            if (mSingleVehicleBean != null && mSingleVehicleBean.getVehicleLicenseCertificationResult() != null) {
                                mSingleVehicleBean.getVehicleLicenseCertificationResult().setVehicleLength(string);
                            }
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            if (dialog.isShowing()) dialog.dismiss();
                        }
                    })
                    .create();
        }
        vehicleLengthDialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != vehicleKeyBoard)
            vehicleKeyBoard.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != vehicleKeyBoard)
            vehicleKeyBoard.onDestroy();
    }
}
